<template>
  <div>
    <el-row>
      <el-form>
        <el-row>
          <el-col :span="6">
            <el-form-item label="系统">
              <el-select v-model="form.systemId" @change="systemIdChange" filterable clearable>
                <el-option v-for="item in systemList" :label="item.Name" :key="item.ID" :value="item.ID"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="数据表">
              <el-select style="width: 70%" v-model="form.tableIds" filterable clearable multiple reserve-keyword>
                <el-option v-for="item in tableList" :label="item.Name+'('+item.Describe+')'" :key="item.ID"
                           :value="item.ID"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-button type="primary" @click="addTable">添加</el-button>
          </el-col>
        </el-row>
      </el-form>
    </el-row>
    <el-row>
      <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4"  v-for="(item,index) in selectTables" :key="item.ID">
        <el-header style="margin-top: 20px"  >
          <el-row>
            {{item.Name+'('+item.Describe+')'}}
          </el-row>
          <el-row style="margin-top: 5px">
            <el-button @click="showGQL(item)" size="small" type="primary">GQL</el-button>
            <el-button @click="removeTable(index)"  size="small" type="danger">移除</el-button>
          </el-row>
        </el-header>
        <el-table :data="item.Columns"
                  border
                  fit
                  size="small"
                  highlight-current-row
                  height="350px"
                  style="width: 100%;">
          <el-table-column type="expand">
            <template slot-scope="props">
              <el-form label-position="left" label-width="120px" class="demo-table-expand">
                <el-form-item label="数据列名">
                  <span>{{ props.row.Name }}</span>
                </el-form-item>
                <el-form-item label="数据列描述">
                  <span>{{ props.row.Describe }}</span>
                </el-form-item>
                <el-form-item label="数据类型">
                  <span>{{ props.row.Type }}</span>
                </el-form-item>
                <el-form-item label="是否主键">
                  <span>{{ props.row.IsMain }}</span>
                </el-form-item>
                <el-form-item label="是否允许为空">
                  <span>{{ props.row.IsEmptyable }}</span>
                </el-form-item>
                <el-form-item label="字典类型">
                  <span>{{ props.row.DictType }}</span>
                </el-form-item>
                <el-form-item label="字典子系统">
                  <span>{{ props.row.SubDictSystem }}</span>
                </el-form-item>
                <el-form-item label="字典组标识">
                  <span>{{ props.row.DictGroupFlag }}</span>
                </el-form-item>
                <el-form-item label="值对应字典列">
                  <span>{{ props.row.DictColumn }}</span>
                </el-form-item>
                <el-form-item label="字典子类型">
                  <span>{{ props.row.SubDictType }}</span>
                </el-form-item>
                <el-form-item label="数据列大小">
                  <span>{{ props.row.ColumnSize }}</span>
                </el-form-item>
                <el-form-item label="数据小数位">
                  <span>{{ props.row.ColumnSuffix }}</span>
                </el-form-item>
              </el-form>
            </template>
          </el-table-column>
          <el-table-column label="数据列名" prop="Name" sortable show-tooltip-when-overflow>

          </el-table-column>
          <el-table-column label="数据列描述" prop="Describe" sortable show-tooltip-when-overflow></el-table-column>
        </el-table>
      </el-col>

    </el-row>
    <el-dialog :visible.sync="gqlDialog.show" title="gql">
      <div>  "{{gqlDialog.tableInfo.Describe}}"</div>
      <div>type {{gqlDialog.tableInfo.Name | camel}} {</div>
      <div v-for="item in gqlDialog.tableInfo.Columns" :key="item.Name">
        <div> "{{item.Describe}}"</div>
        <div>    {{item.Name | lowerCase}}:
          <span v-if="item.Type ==='实数'">Float</span>
          <span v-else-if="item.Type ==='长整数' || item.Type === '整数'">Int</span>
          <span v-else-if="item.Type === '日期/时间'">DateTime</span>
          <span v-else>String</span>
        </div>
      </div>
      <div>}</div>
    </el-dialog>

  </div>
</template>

<script>
const camelCase = require('camelcase');

export default {
  name: 'app',
  data() {
    return {
      list: [],
      count: 0,
      form: {
        systemId: null,
        tableId: null,
        tableIds: []
      },
      enums:{
        compareType:{
          SAME: 1,
          LEFT_MORE: 2,
          RIGHT_MORE:3
        }
      },
      gqlDialog:{
        show: false,
        tableInfo: {}
      },
      systemList: [],
      tableList: [],
      selectTables: []
    }
  },
  filters:{
    camel(i){
      if(!i){
        return ''
      }
      return camelCase(i, {pascalCase: true})
    },
    lowerCase(i){
      if(!i){
        return ''
      }
      return i.toLowerCase()
    }
  },
  computed:{
    // 当前选中的表信息
    currentTableInfo(){
      if(!this.form.tableId){
        return null
      }
      for(var item of this.tableList){
        if(item.ID === this.form.tableId){
          return item
        }
      }
      return null
    }
  },
  mounted() {
    this.getSystemList()
  },
  methods: {
    getSystemList() {
      this.$axios
          .get("/api/system/list", {
            params: {}
          }, {})
          .then((res) => {
            if (res.data.code === 200) {
              this.systemList = res.data.data
            } else {
              this.$message.error(res.data.msg)
            }
            console.log(res);
          });
    },
    systemIdChange() {
      this.form.tableId = null
      this.form.tableIds = []
      var postData = {}
      postData.systemId = this.form.systemId
      this.$axios
          .get("/api/table/list", {params: postData}, {})
          .then((res) => {
            if (res.data.code === 200) {
              this.tableList = res.data.data
            } else {
              this.$message.error(res.data.msg)
            }
            console.log(res);
          });
    },
    async addTable() {
      if (!this.form.tableIds) {
        this.$message.error("请选择数据表");
        return
      }
      for (var tableId of this.form.tableIds) {
        if (this.isTableExist(tableId)) {
          continue
        }
        var tableInfo = this.getTableInfo(tableId)
        var postData = {}
        postData.tableId = tableId
        var res = await this.$axios.get("/api/column/list", {params: postData}, {})
        if (res.data.code === 200) {
          tableInfo.Columns = res.data.data
          this.selectTables.push(tableInfo)
        } else {
          this.$message.error(res.data.msg)
        }
      }
    },
    removeTable(index){
      this.selectTables.splice(index, 1)
    },
    showGQL(item){
      this.gqlDialog.tableInfo = item
      this.gqlDialog.show = true
    },
    getTableInfo(tableId){
      if(!this.tableList){
        return null
      }
      for (var item of this.tableList){
        if(item.ID === tableId){
          return item
        }
      }
      return null
    },
    isTableExist(tableId){
      if(!this.selectTables){
        return false
      }
      for(var item of this.selectTables){
        if(item.ID === tableId){
          return true
        }
      }
      return false
    }
  }
}
</script>

<style>

#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}

.demo-table-expand {
  font-size: 0;
}

.demo-table-expand label {
  color: #99a9bf;
}

.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 80%;
}
</style>
